package com.liutaw.nanjingair.views.viewcomponent;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.liutaw.nanjingair.R;


/**
 * Created by liutao on 15/12/21.
 */
public class TopTipView {

    private Activity activity;
    private View view;
    private ViewGroup decorView;
    private boolean isShowing = false;
    private ImageView leftImg;
    private TextView textView;
    private long annimationTime = -1L;

    public TopTipView(Activity activity) {
        this.activity = activity;
        init();
    }

    private void init() {
        decorView = (ViewGroup) this.activity.getWindow().getDecorView().findViewById(android.R.id.content);
        view = LayoutInflater.from(activity).inflate(R.layout.layout_tooltipview, decorView, false);
        textView = (TextView) view.findViewById(R.id.text_info);
        leftImg = (ImageView) view.findViewById(R.id.img_left);
    }

    public void show(String showText) {
        //开始动画
        if (isShowing) {
            return;
        }
        isShowing = true;
        textView.setText(showText);

        if (view.getParent() == null) {
            decorView.addView(view);
            view.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.slide_in_top));
        }
        if (annimationTime == -1L) {
            annimationTime = 1300;
        }
        decorView.postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        }, annimationTime);
    }

    public void dismiss() {
        if (!isShowing) {
            return;
        }
        Animation out = AnimationUtils.loadAnimation(activity, R.anim.slide_out_top);
        out.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                decorView.post(new Runnable() {
                    @Override
                    public void run() {
                        if (view.getParent() != null) {
                            decorView.removeView(view);
                        }
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(out);
        isShowing = false;
    }

    public boolean isShowing() {
        return isShowing;
    }

    public void setIsShowing(boolean isShowing) {
        this.isShowing = isShowing;
    }

    public void setImgResId(int imgResId) {
        leftImg.setImageResource(imgResId);
    }

    public long getAnnimationTime() {
        return annimationTime;
    }

    public void setAnnimationTime(long annimationTime) {
        this.annimationTime = annimationTime;
    }
}
